package servlet.MaterialServlet;

import dao.MaterialDao;
import entity.MaterialBean;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/MaterialSaveServlet")
public class MaterialSaveServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        MaterialDao materialDao = new MaterialDao();
        String I = request.getParameter("id");
        MaterialBean obj = new MaterialBean();
        int curpage = 0;
        int pageCount = 1;
        int id=0;
        if(request.getParameter("curpage")!=null)
            curpage=Integer.parseInt(request.getParameter("curpage"));
        if(request.getParameter("pageCount")!=null)
            pageCount=Integer.parseInt(request.getParameter("pageCount"));
        System.out.println("materialSaveServlet pageCount="+pageCount);
        System.out.println("materialSaveServlet curpage="+curpage);
        String itemId = request.getParameter("itemId");
        String itemName = request.getParameter("itemName");
        String itemClasses = request.getParameter("itemClasses");
        String vendorName = request.getParameter("vendorName");
        String site = request.getParameter("site");
        String inboundTime = request.getParameter("inboundTime");
        String contact = request.getParameter("contact");
        String phoneNumber = request.getParameter("phoneNumber");
        String itemDescription = request.getParameter("itemDescription");
        obj.setId(0);
        obj.setItemId("");
        obj.setItemName("");
        obj.setItemClasses("");
        obj.setItemDescription("");
        obj.setVendorName("");
        obj.setContact("");
        obj.setSite("");
        obj.setInboundTime("");
        obj.setPhoneNumber("");
        obj.setItemId(itemId);
        obj.setItemName(itemName);
        obj.setItemClasses(itemClasses);
        obj.setItemDescription(itemDescription);
        obj.setVendorName(vendorName);
        obj.setContact(contact);
        obj.setSite(site);
        obj.setInboundTime(inboundTime);
        obj.setPhoneNumber(phoneNumber);
        if (I == null || I == "")
            id=0;
        else
            id = Integer.parseInt(I);
        request.setAttribute("obj", obj);
        request.setAttribute("curpage",curpage);
        request.setAttribute("pageCount",pageCount);
        System.out.println("SaveServlet I:"+I);
        if (id == 0) {
            try {
                if(materialDao.addMaterial(itemId, itemName, itemClasses,vendorName, site, inboundTime, contact, phoneNumber, itemDescription))
                    request.getRequestDispatcher("MaterialListServlet").forward(request,response);
                else{
                    request.setAttribute("editBackFlag",1);
                    request.getRequestDispatcher("material_edit.jsp").forward(request, response);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } else {
            try {
                if (materialDao.editMaterial(id,itemId, itemName, itemClasses, vendorName, site, inboundTime, contact, phoneNumber, itemDescription)) {
                    request.getRequestDispatcher("MaterialListServlet").forward(request,response);
                } else {
                    request.setAttribute("editBackFlag",1);
                    request.getRequestDispatcher("material_edit.jsp").forward(request, response);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
